import settings
import bsddb
import struct
import sys


class Stat:
    class TLD:
        def __init__(self):
            # Global domain
            self.com = 0
            self.net = 0
            self.org = 0

            # TH domain
            self.inth = 0
            self.coth = 0
            self.acth = 0
            self.goth = 0

    def __init__(self):
        self.tot_site = 0
        self.tot_webpage = 0
        self.tot_hyperlink = 0
        self.tot_hashurl = 0
        self.cum_pagesize = 0
        
        # Bowtie
        self.inlink = 0
        self.outlink = self.tot_hyperlink
        self.scc = 0
        self.dendrite = 0
        self.disconnected = 0       # dangling node

    def dump(self):
        db = bsddb.btopen(settings.db_stat,'c')
        db['total_site'] = str(self.tot_site)
        db['total_webpage'] = str(self.tot_webpage)
        db['total_hyperlink'] = str(self.tot_hyperlink)
        db['total_hashurl'] = str(self.tot_hashurl)
        db['cum_pagesize'] = str(self.cum_pagesize)
        db['inlink'] = str(self.inlink)
        db['outlink'] = str(self.tot_hyperlink)
        db['avg_pagesize'] = str(1.0*self.cum_pagesize/self.tot_webpage)

class Database:
    def __init__(self):
        self.urlid = bsddb.btopen(settings.db_urlid,'c')
        self.idurl = bsddb.btopen(settings.db_idurl,'c')
        self.webgraph = open(settings.db_webgraph,'wb')
        self.transposegraph = {}

        self.urlid['totalurl'] = '0'
        self.totalurl = 0 
    def addurl(self,url):
        if not self.urlid.has_key(url) :
            self.totalurl = id = self.totalurl + 1
            self.urlid['totalurl'] = str(id)
            self.urlid[str(url)] = str(id)
            self.urlid.sync()

            self.idurl[str(id)] = str(url)
            self.idurl.sync()
            return id
        else:
            return int(self.urlid[str(url)])
    def addnode(self,src,dest):
        data = struct.pack('i',src)
        self.webgraph.write(data)

        data = struct.pack('i',len(dest))
        self.webgraph.write(data)

        for d in dest:
            data = struct.pack('i',d)
            self.webgraph.write(data)

            # Transpose graph
            if self.transposegraph.has_key(int(d)) :
                if str(src) not in self.transposegraph[str(d)] :
                    self.transposegraph[str(d)].append(str(src))
            else:
                self.transposegraph[str(d)] = [str(src)]
    def createtransposegraph(self):
        tpgraph = bsddb.btopen(settings.db_transposegraph,'c')
        for dest in self.transposegraph:
            src_list = self.transposegraph[dest]
            tpgraph[dest] = ' '.join(src_list)
            tpgraph.sync()
        tpgraph.close()

    def addhost(self,hostid,pageid_list):
        sitegraph = bsddb.btopen(settings.db_sitegraph,'c')
        sitegraph[str(hostid)] = ' '.join(pageid_list)
        sitegraph.sync()
        sitegraph.close()

    def addtitle(self,src,title):
        dbtitle = bsddb.btopen(settings.db_title,'c')
        dbtitle[str(src)] = str(title)
        dbtitle.sync()
        dbtitle.close()

    def close(self):
        self.urlid.close()
        self.webgraph.close()
